﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class ProductType : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        #region Private Method

        private void BindGridView()
        {
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();
            int customerTypeId = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["MDProductType"].ToString());
            OperationResult<List<tb_MasterData>> result = masterDataServiceObj.GetMasterDataByTypeId(customerTypeId);

            if (result.operationSuccess)
            {
                gvCustomerTypeList.DataSource = result.obj;
                gvCustomerTypeList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();
            OperationResult<tb_MasterData> resultEdit = masterDataServiceObj.GetMasterDateById(id);
            if (resultEdit.operationSuccess == true)
            {
                txtEditName.Text = resultEdit.obj.Name;
                txtEditDescription.Text = resultEdit.obj.Description;
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();

            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();

            tb_MasterData instance = new tb_MasterData();

            instance.Name = txtName.Text;
            instance.Description = txtDescription.Text;
            instance.TypeId = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["MDProductType"].ToString());

            OperationResult<tb_MasterData> result = masterDataServiceObj.CreateMasterData(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();

            tb_MasterData entity = new tb_MasterData();

            entity.Id = editId;
            entity.Name = txtEditName.Text;
            entity.Description = txtEditDescription.Text;
            entity.TypeId = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["MDProductType"].ToString());

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_MasterData> resultEdit = masterDataServiceObj.ModifyMasterData(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();
            tb_MasterData deleteEntity = new tb_MasterData();
            deleteEntity.Id = deleteId;
            masterDataServiceObj.DeleteMasterData(deleteEntity);

            BindGridView();
        }

        protected void gvCustomerTypeList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvCustomerTypeList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomerTypeList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
